package com.douqu.game.activity.entity;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.activity.GameServer;
import com.douqu.game.activity.entity.camp.StartCampBattleParam;
import com.douqu.game.core.config.StableDataConfig;
import com.douqu.game.core.e.E_ServerType;
import com.douqu.game.core.e.E_StableDataType;
import com.douqu.game.core.entity.activity.MatchBaseInfo;
import com.douqu.game.core.entity.battle.BattleDetail;
import com.douqu.game.core.entity.battle.BattleInitParam;
import com.douqu.game.core.factory.CodeFactory;
import com.douqu.game.core.factory.ConfigFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.netty.NettyConnection;
import com.douqu.game.core.util.LogUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-03-09 15:37
 */
public class BattleServerInfo {

    private NettyConnection connection;

    private String host;

    private int port;

    /**
     * 请求了战斗还没连接战斗服务器的
     */
    private Map<String, StartCampBattleParam> startBattleParamMap = new ConcurrentHashMap<>();

    private List<BattleDetail> battleList = new CopyOnWriteArrayList<>();

    private Map<String, BattleDetail> battleMap = new ConcurrentHashMap<>();

    public BattleServerInfo(NettyConnection connection, int port)
    {
        connection.setServerType(E_ServerType.BATTLE);
        this.connection = connection;
        this.port = port;
        this.host = GameServer.getInstance().getActivityCampManager().getActivityCampModel().getServerHost();
    }

    public void update()
    {
        for(Map.Entry<String, StartCampBattleParam> entry : startBattleParamMap.entrySet())
        {
            if(entry.getValue().isOverdue())
            {
                LogUtils.warn("通知了战斗服务器,一直没有收到回复,删除 -> " + entry.getValue());
                removeStartBattleParam(entry.getKey());
            }
        }
    }

    /**
     * 向战斗服务器发送战斗创建消息
     * @param battleId
     * @param battleType
     */
    public void sendBattleInit(String battleId, int battleType, int battleDetailType, MatchBaseInfo player, MatchBaseInfo target)
    {
        if(connection == null)
            return;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(CodeFactory.KEY_BATTLE_ID, battleId);
        jsonObject.put(CodeFactory.KEY_BATTLE_TYPE, battleType);
        jsonObject.put(CodeFactory.KEY_BATTLE_DETAIL_TYPE, battleDetailType);
        jsonObject.put(CodeFactory.KEY_PLAYER_INDEX, player.getObjectIndex());
        jsonObject.put(CodeFactory.KEY_PLAYER_SERVER_ID, player.getServerId());
        jsonObject.put(CodeFactory.KEY_PLAYER_FC, player.getFc());
        jsonObject.put(CodeFactory.KEY_TARGET_INDEX, target.getObjectIndex());
        jsonObject.put(CodeFactory.KEY_TARGET_SERVER_ID, target.getServerId());
        jsonObject.put(CodeFactory.KEY_TARGET_FC, target.getFc());
        jsonObject.put(CodeFactory.KEY_TARGET_NAME, target.getName());

        StableDataConfig stableDataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.CAMP_BATTLE_MAP.getCode());
        if(stableDataConfig == null)
            stableDataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.ARENA_MAP.getCode());

        if(stableDataConfig != null)
            jsonObject.put(CodeFactory.KEY_PARAM, JSONObject.toJSONString(new BattleInitParam(stableDataConfig.intValue)));
        else
            jsonObject.put(CodeFactory.KEY_PARAM, JSONObject.toJSONString(new BattleInitParam(1)));

        ByteBuffer buffer = new ByteBuffer();

        buffer.writeUTF(jsonObject.toJSONString());
        connection.sendMsg(CodeFactory.BATTLE_INIT, buffer.getBytes());

        LogUtils.info("通知战斗服务器创建战斗了 -> battleId:" + battleId + " host:" + connection.getChannel().remoteAddress() + " " + battleDetailType);
    }

    public void sendLogLevel()
    {
        ByteBuffer buffer = new ByteBuffer();
        buffer.writeUTF(ConfigFactory.gmSetting.getActivityBattleLog().name());
        buffer.writeBoolean(ConfigFactory.gmSetting.isBattleTimeoutToAI());

        connection.sendMsg(CodeFactory.UPDATE_LOG_CONFIG, buffer.getBytes());
    }

    public void sendUpdateConfig()
    {
        connection.sendMsg(CodeFactory.LOAD_DATA, null);
    }



    public void addStartBattleParam(StartCampBattleParam param)
    {
        startBattleParamMap.put(param.getBattleId(), param);
    }

    public StartCampBattleParam getStartBattleParam(String battleId)
    {
        return startBattleParamMap.get(battleId);
    }

    public void removeStartBattleParam(String battleId)
    {
        startBattleParamMap.remove(battleId);
    }

    public void addBattle(BattleDetail battle)
    {
        if(battleMap.get(battle.getId()) == null)
        {
            battleMap.put(battle.getId(), battle);
            battleList.add(battle);
        }
    }

    public void removeBattle(BattleDetail battle)
    {
        battleMap.remove(battle.getId());
        for(BattleDetail battleData : battleList)
        {
            if(battleData.getId().equals(battle.getId()))
            {
                battleList.remove(battleData);
                break;
            }
        }

    }

    public BattleDetail getBattle(String battleId)
    {
        return battleMap.get(battleId);
    }



    public void destroy()
    {


    }


    public int getBattleCount() {
        return battleList.size();
    }

    public List<BattleDetail> getBattleList()
    {
        return battleList;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public NettyConnection getConnection() {
        return connection;
    }

    @Override
    public String toString() {
        return "{" +
                "host='" + host + '\'' +
                ", port=" + port +
                '}';
    }
}
